//
//  GoodsOrderView.swift
//  Box
//
//  Created by mn on 2025/4/4.
//

import UIKit
import SnapKit

class GoodsOrderView: UIView {

    var goodsModel: GoodsModel?{
        didSet{
            guard let goodsModel  else { return }
            productView.titleLabel.text = goodsModel.name
            productView.priceLabel.text = String(format: "%.2f", Float(goodsModel.price) * 0.01)
            productView.priceLabel.textColor = .gold
            productView.priceLabel.font = .systemFont(ofSize: 20, weight: .medium)
            productView.productImageView.kf.setImage(with:URL(string:goodsModel.imgUrlList.first ?? ""))
            specificationCountLabel.text = "\(goodsModel.count)"
            priceLabel.text = String(format: "%.2f", Float(goodsModel.price) * 0.01)

        }
    }
    
    private func createLabel(text: String, font: UIFont = UIFont.systemFont(ofSize: 15), color: UIColor = .init(hex: "#333333")) -> UILabel {
        let label = UILabel()
        label.font = font
        label.textColor = color
        label.text = text
        return label
    }

    lazy var titleLabel = createLabel(text: "请选择规格", font: UIFont.systemFont(ofSize: 18, weight: .heavy), color: .fromHex("#22222"))
    let productView = ProductView()
    
    lazy var specificationLabel = createLabel(text: "规格")
    lazy var specificationCountLabel = createLabel(text: "", font: UIFont.systemFont(ofSize: 12), color: .gold)

    lazy var quantityLabel = createLabel(text: "数量")
    lazy var quantitySelectorView = QuantitySelectorView()
    
    lazy var extractionMethodLabel = createLabel(text: "提取方式")
    lazy var warehouseLabel = createLabel(text: "提至仓库")
    
    lazy var totalLabel = createLabel(text: "合计：")
    lazy var priceLabel = createLabel(text: "", font: UIFont.systemFont(ofSize: 16, weight: .bold), color: .gold)
    let buyButton: UIButton = {
        let button = UIButton()
        button.setTitle("立即购买", for: .normal)
        button.setTitleColor(.white, for: .normal)
        button.backgroundColor = .gold
        button.layer.cornerRadius = 20
        return button
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupView()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupView()
    }
    
    private func setupView() {
        layer.cornerRadius = 32
        layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
        backgroundColor = .white
        addSubview(titleLabel)
        addSubview(productView)

        titleLabel.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(30)
            make.centerX.equalToSuperview()
        }
        productView.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(16)
            make.left.right.equalToSuperview()
        }
        //规格
        addSubview(specificationLabel)
        addSubview(specificationCountLabel)
        specificationLabel.snp.makeConstraints { make in
            make.top.equalTo(productView.snp.bottom).offset(16)
            make.left.equalToSuperview().offset(16)
        }
        specificationCountLabel.snp.makeConstraints { make in
            make.top.equalTo(specificationLabel.snp.bottom).offset(3)
            make.left.equalToSuperview().offset(16)
        }
        
        //数量
        addSubview(quantityLabel)
        addSubview(quantitySelectorView)
        quantityLabel.snp.makeConstraints { make in
            make.top.equalTo(specificationCountLabel.snp.bottom).offset(16)
            make.left.equalToSuperview().offset(16)
        }
        quantitySelectorView.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-16)
            make.centerY.equalTo(quantityLabel)
            make.height.equalTo(30)
        }
        quantitySelectorView.countChanged = {
            [weak self] count in
            guard let self,let goodsModel else { return }
            priceLabel.text = String(format: "%.2f", Float(goodsModel.price * count) * 0.01)

        }
        //提取方式
        addSubview(extractionMethodLabel)
        addSubview(warehouseLabel)
        extractionMethodLabel.snp.makeConstraints { make in
            make.top.equalTo(quantityLabel.snp.bottom).offset(26)
            make.left.equalToSuperview().offset(16)
        }
        warehouseLabel.snp.makeConstraints { make in
            make.centerY.equalTo(extractionMethodLabel.snp.centerY)
            make.right.equalToSuperview().offset(-16)
        }
        
        //合计
        addSubview(totalLabel)
        addSubview(priceLabel)
        addSubview(buyButton)

        totalLabel.snp.makeConstraints { make in
            make.top.equalTo(extractionMethodLabel.snp.bottom).offset(30)
            make.left.equalToSuperview().offset(16)
            make.bottom.equalToSuperview().offset(-30)
        }

        priceLabel.snp.makeConstraints { make in
            make.left.equalTo(totalLabel.snp.right).offset(8)
            make.centerY.equalTo(totalLabel)
        }

        buyButton.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-16)
            make.centerY.equalTo(totalLabel)
            make.width.equalTo(120)
            make.height.equalTo(40)
        }
    }

}
